package com.warehouse.controller;

import com.warehouse.dto.Result;
import com.warehouse.dto.ResultCode;
import com.warehouse.entity.Customer;
import com.warehouse.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.ViewportUI;
import java.util.List;

/**
 * @program: warehouse
 * @description: 客户表现层
 * @author: xiaojiu
 * @create: 2021-01-13 22:44
 */
@RestController
@Api(tags = "客户表现层")
@RequestMapping("/api")
public class CustomerController {
    @Autowired(required = false)
    private CustomerService customerService;
    /**
     * 分页查询所有客户
     * @return
     */
    @ApiOperation(value = "查询客户信息",httpMethod = "GET")
    @GetMapping("/findAllCustomers")
    public Result findAllCustomers(@RequestParam Integer currentPage,@RequestParam Integer pageSize ){
        System.out.println(currentPage);
        List<Customer> customers=customerService.findAllCustomers(currentPage,pageSize);
        if(customers!=null){
            return new Result(ResultCode.SUCCESS,customers);
        }
        return new Result(ResultCode.FAIL);

    }

    /**
     * 添加客户信息
     * @param customer
     * @return
     */

    @ApiOperation(value = "添加客户信息",httpMethod = "POST")
    @PostMapping("/addCustomers")
    public Result addCustomers(@RequestBody Customer customer ){
       boolean c =customerService.addCustomers(customer);
        if(c){
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);

    }
    /**
     * 根据编号删除客户信息
     */
    @ApiOperation(value = "根据编号删除客户信息",httpMethod = "DELETE")
   @RequestMapping(value = "/delCustomer",method = RequestMethod.DELETE)
    public Result delCustomers(String id){
        boolean c =customerService.delCustomers(id);
        if(c){
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);

    }
    /**
     * 修改客户
     */
    @ApiOperation(value = "修改客户信息",httpMethod = "PUT")
    @RequestMapping(value = "/editCustomer",method = RequestMethod.PUT)
    public Result editCustomer(@RequestBody Customer customer){

        //根据条件查询修改对象是否存在
        Customer customer1=customerService.findOneBy(customer.getCuntomNum());
        System.out.println(customer1);
        boolean c =customerService.editCustomer(customer);
        if(c){
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);

    }
    /**
     * 模糊查询
     */
    @ApiOperation(value = "根据条件模糊客户信息",httpMethod = "POST")
    @RequestMapping(value = "/findCustomer",method = RequestMethod.POST)
    public Result findCustomer(@RequestBody Customer customer) {
        List<Customer> customers = customerService.findCustomer(customer);
        System.out.println(customers);
        if (customers != null) {
            return new Result(ResultCode.SUCCESS, customers);
        }
        return new Result(ResultCode.FAIL);
    }

}
